package com.gjj.abox.login;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gjj.abox.BaseActivity;
import com.gjj.abox.custom.MessageDialog;
import com.gjj.abox.database.BoxDB;
import com.gjj.abox.database.R;
import com.gjj.abox.entity.User;
import com.gjj.abox.utils.AnimalUtils;
import com.gjj.abox.utils.ToastUtil;


public class BackPasswordActivity extends BaseActivity implements OnClickListener {
    // 根据密保问题找回密码的Activity
    private TextView tvQuestion;
    private EditText etAnswer;
    private Button btnSum;
    private ImageView ivBack;
    private User user;
    private String name, question;
    private final String PREFERENCE_NAME = "source";
    private SharedPreferences sharedpreferences = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.look_back_passwork);
        tvQuestion = ((TextView) findViewById(R.id.tv_look_back_pwd_question));
        etAnswer = ((EditText) findViewById(R.id.et_look_back_pwd_answer));
        btnSum = ((Button) findViewById(R.id.btn_look_back_pwd_sum));
        ivBack = (ImageView) findViewById(R.id.iv_look_back_pwd_back);
        sharedpreferences = getSharedPreferences(PREFERENCE_NAME,
                Activity.MODE_PRIVATE);
        name = sharedpreferences.getString("name", "");
        BoxDB boxDb = new BoxDB(getApplicationContext());
        user = boxDb.queryUserAll(name);
        question = user.getUserQuestion();
        tvQuestion.setText(question);
        ivBack.setOnClickListener(this);
        btnSum.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_look_back_pwd_back:
                finish();
                overridePendingTransition(0, R.anim.slide_right_out);
                break;
            case R.id.btn_look_back_pwd_sum:
                if (etAnswer.getText().toString().equals(user.getUserKey())) {
                    MessageDialog.showTipsDialog(BackPasswordActivity.this, "你的密码是:", user.getUserPwd());
                } else {
                    ToastUtil.showShortToast(BackPasswordActivity.this, "你的答案不正确！");
                    AnimalUtils.shakeAnimal(etAnswer);
                }
                break;
        }
    }
}
